import { cmdExec, getCurrentVersion, getPrefix } from './index';
import { appCmd } from './cmd';
import { compareVersions } from 'compare-versions';
import { join } from 'path';

export const getBundle = async (devId: string) => {
  let bundle: string;
  try {
    const SPVersion = await getCurrentVersion(devId, 'com.ohos.smartperf');
    if (SPVersion && compareVersions(SPVersion, '1.0.2') >= 0) {
      bundle = 'com.ohos.smartperf';
    } else {
      const currentVersion = await getCurrentVersion(
        devId,
        'com.ohos.editorapp',
      );
      if (!currentVersion || compareVersions(currentVersion, '2.1.0') < 0) {
        // 重新安装应用
        await cmdExec(appCmd.hdc.unstallEditorTool(devId));
        const prefix = getPrefix();
        const appPath = join(prefix, '/editorTool/entry-default-signed.hap');
        await cmdExec(appCmd.hdc.installEditorTool(devId, appPath));
      }
      bundle = 'com.ohos.editorapp';
    }
  } catch (err) {
    console.log('getBundle err', err);
  }
  return bundle;
};
